<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go语言结构体 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/79.2c7d6b79.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/learn-go/10 从零搭建Go语言开发环境.html" class="sidebar-link">从零搭建Go语言开发环境</a></li><li><a href="/go/learn-go/11 第一个Go程序.html" class="sidebar-link">第一个Go程序Hello World</a></li><li><a href="/go/learn-go/12 Go 语言数据类型.html" class="sidebar-link">Go语言数据类型</a></li><li><a href="/go/learn-go/13 Go语言变量.html" class="sidebar-link">Go语言变量</a></li><li><a href="/go/learn-go/14 Go语言常量.html" class="sidebar-link">Go语言常量</a></li><li><a href="/go/learn-go/15 Go 语言运算符.html" class="sidebar-link">Go语言运算符</a></li><li><a href="/go/learn-go/16 Go语言条件语句.html" class="sidebar-link">Go语言条件语句</a></li><li><a href="/go/learn-go/17 Go语言的循环语句.html" class="sidebar-link">Go语言的循环语句</a></li><li><a href="/go/learn-go/18 Go 语言数组.html" class="sidebar-link">Go语言数组</a></li><li><a href="/go/learn-go/19 Go语言切片.html" class="sidebar-link">Go语言基础之切片</a></li><li><a href="/go/learn-go/20 Go语言map.html" class="sidebar-link">Go语言基础之map</a></li><li><a href="/go/learn-go/21 Go语言指针.html" class="sidebar-link">Go语言指针</a></li><li><a href="/go/learn-go/22 Go语言函数.html" class="sidebar-link">Go语言函数</a></li><li><a href="/go/learn-go/23  fmt.Printf总结.html" class="sidebar-link">fmt.Printf总结</a></li><li><a href="/go/learn-go/24 Go语言常用方法总结.html" class="sidebar-link">Go常用方法总结</a></li><li><a href="/go/learn-go/25 Go语言结构体.html" class="active sidebar-link">Go语言结构体</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_1-类型别名和自定义类型" class="sidebar-link">1 类型别名和自定义类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_1-1-自定义类型" class="sidebar-link">1.1 自定义类型</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_1-2-类型别名" class="sidebar-link">1.2 类型别名</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_1-3-类型定义和类型别名的区别" class="sidebar-link">1.3 类型定义和类型别名的区别</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_2-结构体" class="sidebar-link">2 结构体</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_2-1-结构体的定义语法" class="sidebar-link">2.1 结构体的定义语法</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_2-2-结构体定义的三种形式" class="sidebar-link">2.2 结构体定义的三种形式</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_2-3-结构体实例化" class="sidebar-link">2.3 结构体实例化</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_3-匿名结构体" class="sidebar-link">3 匿名结构体</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_4-结构体指针" class="sidebar-link">4 结构体指针</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_4-1-创建指针类型结构体" class="sidebar-link">4.1 创建指针类型结构体</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_4-2-取结构体的地址实例化" class="sidebar-link">4.2 取结构体的地址实例化</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_5-结构体作为函数参数" class="sidebar-link">5 结构体作为函数参数</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_6-构造函数" class="sidebar-link">6 构造函数</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_7-结构体的匿名字段" class="sidebar-link">7 结构体的匿名字段</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_8-嵌套结构体" class="sidebar-link">8 嵌套结构体</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_9-嵌套匿名结构体" class="sidebar-link">9 嵌套匿名结构体</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_10-嵌套结构体的字段名冲突" class="sidebar-link">10 嵌套结构体的字段名冲突</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_11-结构体与json序列化" class="sidebar-link">11 结构体与JSON序列化</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/25 Go语言结构体.html#_12-结构体标签（tag）" class="sidebar-link">12 结构体标签（Tag）</a></li></ul></li><li><a href="/go/learn-go/26 Go语言方法.html" class="sidebar-link">Go语言方法</a></li><li><a href="/go/learn-go/27 Go语言接口.html" class="sidebar-link">Go语言接口</a></li><li><a href="/go/learn-go/28 Go语言并发编程.html" class="sidebar-link">Go语言并发编程</a></li><li><a href="/go/learn-go/29 Go语言文件操作.html" class="sidebar-link">Go语言文件操作</a></li><li><a href="/go/learn-go/30 Go语言单元测试.html" class="sidebar-link">Go语言单元测试</a></li><li><a href="/go/learn-go/31 Go语言之http.html" class="sidebar-link">Go语言之http</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go工具中间件</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go语言结构体"><a href="#go语言结构体" class="header-anchor">#</a> Go语言结构体</h1> <h2 id="_1-类型别名和自定义类型"><a href="#_1-类型别名和自定义类型" class="header-anchor">#</a> 1 类型别名和自定义类型</h2> <h3 id="_1-1-自定义类型"><a href="#_1-1-自定义类型" class="header-anchor">#</a> 1.1 自定义类型</h3> <blockquote><p>在Go语言中有一些基本的数据类型，如string、整型、浮点型、布尔等数据类型， Go语言中可以使用type关键字来定义自定义类型。
自定义类型是定义了一个全新的类型。我们可以基于内置的基本类型定义，也可以通过struct定义。例如：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//将MyInt定义为int类型</span>
<span class="token keyword">type</span> MyInt <span class="token builtin">int</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>通过type关键字的定义，MyInt就是一种新的类型，它具有int的特性</p> <h3 id="_1-2-类型别名"><a href="#_1-2-类型别名" class="header-anchor">#</a> 1.2 类型别名</h3> <blockquote><p>类型别名是Go1.9版本添加的新功能。
类型别名规定：TypeAlias只是Type的别名，本质上TypeAlias与Type是同一个类型。就像一个孩子小时候有小名、乳名，上学后用学名，英语老师又会给他起英文名，但这些名字都指的是他本人。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code> <span class="token keyword">type</span> TypeAlias <span class="token operator">=</span> Type
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>我们之前见过的rune和byte就是类型别名，他们的定义如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code> <span class="token keyword">type</span> <span class="token builtin">byte</span> <span class="token operator">=</span> <span class="token builtin">uint8</span>
 <span class="token keyword">type</span> <span class="token builtin">rune</span> <span class="token operator">=</span> <span class="token builtin">int32</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_1-3-类型定义和类型别名的区别"><a href="#_1-3-类型定义和类型别名的区别" class="header-anchor">#</a> 1.3 类型定义和类型别名的区别</h3> <p><strong>类型别名与类型定义表面上看只有一个等号的差异，我们通过下面的这段代码来理解它们之间的区别。</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
 <span class="token keyword">import</span> <span class="token punctuation">(</span>
 	<span class="token string">&quot;fmt&quot;</span>
 	<span class="token string">&quot;reflect&quot;</span>
 <span class="token punctuation">)</span>
 <span class="token comment">//类型定义</span>
 <span class="token keyword">type</span> NewInt <span class="token builtin">int</span>
 <span class="token comment">//类型别名</span>
 <span class="token keyword">type</span> MyInt <span class="token operator">=</span> <span class="token builtin">int</span>
 <span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 	<span class="token keyword">var</span> a NewInt
 	<span class="token keyword">var</span> b MyInt
 	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;变量a的类型是:%T\n&quot;</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span> <span class="token comment">//变量a的类型是:main.NewInt</span>
 	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;变量b的类型是:%T\n&quot;</span><span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token comment">//变量b的类型是:int</span>
 	<span class="token comment">//判断a和b类型是否一样</span>
 	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>reflect<span class="token punctuation">.</span><span class="token function">TypeOf</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Kind</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> reflect<span class="token punctuation">.</span><span class="token function">TypeOf</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Kind</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><blockquote><p>结果显示a的类型是main.NewInt，表示main包下定义的NewInt类型。b的类型是int。MyInt类型只会在代码中存在，编译完成时并不会有MyInt类型</p></blockquote> <h2 id="_2-结构体"><a href="#_2-结构体" class="header-anchor">#</a> 2 结构体</h2> <div class="custom-block tip"><p class="custom-block-title">概述</p> <p>Go语言中的基础数据类型可以表示一些事物的基本属性，但是当我们想表达一个事物的全部或部分属性时，这时候再用单一的基本数据类型明显就无法满足需求了，Go语言提供了一种自定义数据类型，可以封装多个基本数据类型，这种数据类型叫结构体，英文名称<code>struct</code>。 也就是我们可以通过<code>struct</code>来定义自己的类型了。类似于java中的class</p></div> <h3 id="_2-1-结构体的定义语法"><a href="#_2-1-结构体的定义语法" class="header-anchor">#</a> 2.1 结构体的定义语法</h3> <p><strong>使用type和struct关键字来定义结构体，具体代码格式如下：</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> 类型名 <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    字段名 字段类型
    字段名 字段类型
    ……
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="custom-block danger"><p class="custom-block-title">注意</p> <ul><li>类型名：标识自定义结构体的名称，在同一个包内不能重复。</li> <li>字段名：表示结构体字段名。结构体中的字段名必须唯一。</li> <li>字段类型：表示结构体字段的具体类型。</li></ul></div> <h3 id="_2-2-结构体定义的三种形式"><a href="#_2-2-结构体定义的三种形式" class="header-anchor">#</a> 2.2 结构体定义的三种形式</h3> <p>第一种【基本的实例化】</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> stu Student
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>第二种【指针类型的结构体】</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> stu <span class="token operator">*</span>Student <span class="token operator">=</span> <span class="token function">new</span><span class="token punctuation">(</span>Student<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>第三种【取结构体的地址实例化,通过&amp;的操作】</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> stu <span class="token operator">*</span>Student <span class="token operator">=</span> <span class="token operator">&amp;</span>Student<span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-3-结构体实例化"><a href="#_2-3-结构体实例化" class="header-anchor">#</a> 2.3 结构体实例化</h3> <p><strong>举例如下</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//学生</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
  Name <span class="token builtin">string</span>   <span class="token comment">//姓名</span>
  Age <span class="token builtin">int</span>       <span class="token comment">//年龄</span>
  Sex <span class="token builtin">bool</span>      <span class="token comment">//性别 true:男    false:女</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="_2-3-1-基本的实例化"><a href="#_2-3-1-基本的实例化" class="header-anchor">#</a> 2.3.1 基本的实例化</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//定义一个变量stu </span>
<span class="token keyword">var</span> stu Student
<span class="token comment">//没有初始化的结构体，其成员变量都是对应其类型的零值。</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu<span class="token punctuation">)</span> <span class="token comment">//{ 0 false}</span>
stu<span class="token punctuation">.</span>Name<span class="token operator">=</span><span class="token string">&quot;张三&quot;</span>    <span class="token comment">//赋值</span>
stu<span class="token punctuation">.</span>Age<span class="token operator">=</span><span class="token number">18</span>
stu<span class="token punctuation">.</span>Sex<span class="token operator">=</span><span class="token boolean">true</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu<span class="token punctuation">)</span> <span class="token comment">//{张三 18 true}</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>   <span class="token comment">//访问字段</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%T&quot;</span><span class="token punctuation">,</span>stu<span class="token punctuation">)</span>  <span class="token comment">//main.Student</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="_2-3-2-键值对初始化结构体"><a href="#_2-3-2-键值对初始化结构体" class="header-anchor">#</a> 2.3.2 键值对初始化结构体</h4> <blockquote><p>键值之间以:分隔；键值对之间以,分隔</p></blockquote> <div class="custom-block tip"><p class="custom-block-title">键值对语法如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>变量名 <span class="token operator">:=</span> 结构体类型名<span class="token punctuation">{</span>
    字段<span class="token number">1</span><span class="token punctuation">:</span> 字段<span class="token number">1</span>的值<span class="token punctuation">,</span>
    字段<span class="token number">2</span><span class="token punctuation">:</span> 字段<span class="token number">2</span>的值<span class="token punctuation">,</span>
    <span class="token operator">...</span><span class="token operator">...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div> <p>键值对初始化结构体举例</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>stu3 <span class="token operator">:=</span> Student<span class="token punctuation">{</span>
  Name<span class="token punctuation">:</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">,</span>
  Age<span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="_2-3-3-值列表填充结构体"><a href="#_2-3-3-值列表填充结构体" class="header-anchor">#</a> 2.3.3 值列表填充结构体</h4> <p>值列表填充结构体【没有字段，按着序列，必须全部填充】</p> <div class="custom-block tip"><p class="custom-block-title">值列表填充结构体语法如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>变量名 <span class="token operator">:=</span> 结构体类型名<span class="token punctuation">{</span>
    字段<span class="token number">1</span>的值<span class="token punctuation">,</span>
    字段<span class="token number">2</span>的值<span class="token punctuation">,</span>
    <span class="token operator">...</span><span class="token operator">...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div> <p>值列表填充结构体举例如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>stu4 <span class="token operator">:=</span> Student<span class="token punctuation">{</span>
  <span class="token string">&quot;王五&quot;</span><span class="token punctuation">,</span>
  <span class="token number">18</span><span class="token punctuation">,</span>
  <span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="_3-匿名结构体"><a href="#_3-匿名结构体" class="header-anchor">#</a> 3 匿名结构体</h2> <blockquote><p>在定义一些临时数据结构等场景下还可以使用匿名结构体。</p></blockquote> <div class="custom-block tip"><p class="custom-block-title">语法如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//注意:匿名结构体这里是var声明变量， 普通结构体是type在函数外部</span>
<span class="token keyword">var</span> 类型名 <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    字段名 字段类型
    字段名 字段类型
    ……
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></div> <p>举例如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 匿名结构体定义</span>
<span class="token keyword">var</span> user <span class="token keyword">struct</span><span class="token punctuation">{</span>
   name <span class="token builtin">string</span>
   age <span class="token builtin">int</span>
   address <span class="token builtin">string</span>
<span class="token punctuation">}</span>
user<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">&quot;福小林&quot;</span>
user<span class="token punctuation">.</span>age<span class="token operator">=</span><span class="token number">29</span>
user<span class="token punctuation">.</span>address<span class="token operator">=</span><span class="token string">&quot;成都&quot;</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span> <span class="token comment">//{福小林 29 成都}</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%T\n&quot;</span><span class="token punctuation">,</span> user<span class="token punctuation">)</span> <span class="token comment">//struct { name string; age int; address string }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="_4-结构体指针"><a href="#_4-结构体指针" class="header-anchor">#</a> 4 结构体指针</h2> <h3 id="_4-1-创建指针类型结构体"><a href="#_4-1-创建指针类型结构体" class="header-anchor">#</a> 4.1 创建指针类型结构体</h3> <p>需要注意的是在Go语言中支持对结构体指针直接使用.来访问结构体的成员。。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> 变量名 <span class="token operator">=</span> <span class="token function">new</span><span class="token punctuation">(</span>结构名<span class="token punctuation">)</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>
<span class="token comment">//学生</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name <span class="token builtin">string</span>   <span class="token comment">//姓名</span>
	Age <span class="token builtin">int</span>       <span class="token comment">//年龄</span>
	Sex <span class="token builtin">bool</span>      <span class="token comment">//性别 true:男    false:女</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建指针类型的机构体变量</span>
<span class="token keyword">var</span> stu1  <span class="token operator">=</span> <span class="token function">new</span><span class="token punctuation">(</span>Student<span class="token punctuation">)</span> <span class="token comment">//*Student</span>
stu1<span class="token punctuation">.</span>Name <span class="token operator">=</span> <span class="token string">&quot;李四&quot;</span> <span class="token comment">//使用结构体指针访问结构体成员，使用 “.” 操作符。</span>
stu1<span class="token punctuation">.</span>Age <span class="token operator">=</span> <span class="token number">20</span> <span class="token comment">//等价于(*stu1).Age=20   Go语言的语法糖，可以使用 “.” 操作结构体成员</span>
stu1<span class="token punctuation">.</span>Sex <span class="token operator">=</span> <span class="token boolean">false</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu1<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%T\n&quot;</span><span class="token punctuation">,</span> stu1<span class="token punctuation">)</span>     <span class="token comment">//*main.Student</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;stu1=%v\n&quot;</span><span class="token punctuation">,</span> stu1<span class="token punctuation">)</span> <span class="token comment">//stu1=&amp;{李四 20 false}</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%p\n&quot;</span><span class="token punctuation">,</span> stu1<span class="token punctuation">)</span>     <span class="token comment">//stu1保存的值是一个内存地址 0xc0000044c0</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%p\n&quot;</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>stu1<span class="token punctuation">)</span>     <span class="token comment">//stu1的内存地址0xc000006028</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_4-2-取结构体的地址实例化"><a href="#_4-2-取结构体的地址实例化" class="header-anchor">#</a> 4.2 取结构体的地址实例化</h3> <p>使用&amp;对结构体进行取地址操作相当于对该结构体类型进行了一次new实例化操作。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>stu3 <span class="token operator">:=</span> <span class="token operator">&amp;</span>Student<span class="token punctuation">{</span><span class="token punctuation">}</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%T\n&quot;</span><span class="token punctuation">,</span> stu3<span class="token punctuation">)</span>     <span class="token comment">//*main.Student</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;stu3=%#v\n&quot;</span><span class="token punctuation">,</span> stu3<span class="token punctuation">)</span> <span class="token comment">//stu3=&amp;main.Student{Name:&quot;&quot;, Age:0, Sex:false}</span>
stu3<span class="token punctuation">.</span>Name<span class="token operator">=</span><span class="token string">&quot;福小林&quot;</span>
stu3<span class="token punctuation">.</span>Age<span class="token operator">=</span><span class="token number">30</span>
stu3<span class="token punctuation">.</span>Sex<span class="token operator">=</span><span class="token boolean">true</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;stu3=%v\n&quot;</span><span class="token punctuation">,</span> stu3<span class="token punctuation">)</span> <span class="token comment">//stu3=&amp;{福小林 30 true}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="_5-结构体作为函数参数"><a href="#_5-结构体作为函数参数" class="header-anchor">#</a> 5 结构体作为函数参数</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> 函数名<span class="token punctuation">(</span><span class="token punctuation">[</span>结构体变量、结构体指针变量<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//函数体</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>举例如下</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//参数是结构体变量</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>stu Student<span class="token punctuation">)</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu<span class="token punctuation">.</span>Name<span class="token punctuation">,</span> <span class="token string">&quot;Hello&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">//参数是结构体指针变量</span>
<span class="token keyword">func</span> <span class="token function">printStudent</span><span class="token punctuation">(</span>stu <span class="token operator">*</span>Student<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu<span class="token punctuation">.</span>Name<span class="token punctuation">,</span> stu<span class="token punctuation">.</span>Age<span class="token punctuation">,</span> stu<span class="token punctuation">.</span>Sex<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="_6-构造函数"><a href="#_6-构造函数" class="header-anchor">#</a> 6 构造函数</h2> <blockquote><p>Go语言的结构体没有构造函数，我们可以自己实现。 例如，下方的代码就实现了一个Student的构造函数。 因为struct是值类型，如果结构体比较复杂的话，值拷贝性能开销会比较大，所以该构造函数返回的是结构体指针类型。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//学生</span>
<span class="token keyword">type</span> Student <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name <span class="token builtin">string</span>   <span class="token comment">//姓名</span>
	Age <span class="token builtin">int</span>       <span class="token comment">//年龄</span>
	Sex <span class="token builtin">bool</span>      <span class="token comment">//性别 true:男    false:女</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">newStudent</span><span class="token punctuation">(</span>Name <span class="token builtin">string</span><span class="token punctuation">,</span>Age <span class="token builtin">int</span><span class="token punctuation">,</span>Sex <span class="token builtin">bool</span><span class="token punctuation">)</span> <span class="token operator">*</span>Student<span class="token punctuation">{</span>

	<span class="token keyword">return</span> <span class="token operator">&amp;</span>Student<span class="token punctuation">{</span>
		Name<span class="token punctuation">:</span>Name<span class="token punctuation">,</span>
		Age<span class="token punctuation">:</span> Age<span class="token punctuation">,</span>
		Sex<span class="token punctuation">:</span>  Sex<span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//调用构造函数赋值</span>
	stu5<span class="token operator">:=</span><span class="token function">newStudent</span><span class="token punctuation">(</span><span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu5<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%p\n&quot;</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>stu5<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>stu5<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="_7-结构体的匿名字段"><a href="#_7-结构体的匿名字段" class="header-anchor">#</a> 7 结构体的匿名字段</h2> <blockquote><p>结构体允许其成员字段在声明时没有字段名而只有类型，这种没有名字的字段就称为匿名字段</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//Person 结构体Person类型</span>
<span class="token keyword">type</span> Person <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	<span class="token builtin">string</span>
	<span class="token builtin">int</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	p1 <span class="token operator">:=</span> Person<span class="token punctuation">{</span>
		<span class="token string">&quot;福小林&quot;</span><span class="token punctuation">,</span>
		<span class="token number">18</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%#v\n&quot;</span><span class="token punctuation">,</span> p1<span class="token punctuation">)</span>        <span class="token comment">//main.Person{string:&quot;福小林&quot;, int:18}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>p1<span class="token punctuation">.</span><span class="token builtin">string</span><span class="token punctuation">,</span> p1<span class="token punctuation">.</span><span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token comment">//福小林 18</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>匿名字段默认采用类型名作为字段名，结构体要求字段名称必须唯一，因此一个结构体中同种类型的匿名字段只能有一个。</p> <h2 id="_8-嵌套结构体"><a href="#_8-嵌套结构体" class="header-anchor">#</a> 8 嵌套结构体</h2> <blockquote><p>一个结构体中可以嵌套包含另一个结构体或结构体指针</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>

<span class="token comment">//Address 地址结构体</span>
<span class="token keyword">type</span> Address <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Province <span class="token builtin">string</span>
	City     <span class="token builtin">string</span>
<span class="token punctuation">}</span>

<span class="token comment">//User 用户结构体</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>
	Gender  <span class="token builtin">string</span>
	Address Address
<span class="token punctuation">}</span>

<span class="token comment">//company 公司结构体</span>
<span class="token keyword">type</span> company <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>
	Address Address
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	user1 <span class="token operator">:=</span> User<span class="token punctuation">{</span>
		Name<span class="token punctuation">:</span>   <span class="token string">&quot;刘强东&quot;</span><span class="token punctuation">,</span>
		Gender<span class="token punctuation">:</span> <span class="token string">&quot;男&quot;</span><span class="token punctuation">,</span>
		Address<span class="token punctuation">:</span> Address<span class="token punctuation">{</span>
			Province<span class="token punctuation">:</span> <span class="token string">&quot;江苏&quot;</span><span class="token punctuation">,</span>
			City<span class="token punctuation">:</span>     <span class="token string">&quot;宿迁&quot;</span><span class="token punctuation">,</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;user1=%#v\n&quot;</span><span class="token punctuation">,</span> user1<span class="token punctuation">)</span>

	company1 <span class="token operator">:=</span> company<span class="token punctuation">{</span>
		Name<span class="token punctuation">:</span>   <span class="token string">&quot;阿里巴巴公司&quot;</span><span class="token punctuation">,</span>
		Address<span class="token punctuation">:</span> Address<span class="token punctuation">{</span>
			Province<span class="token punctuation">:</span> <span class="token string">&quot;浙江&quot;</span><span class="token punctuation">,</span>
			City<span class="token punctuation">:</span>     <span class="token string">&quot;杭州&quot;</span><span class="token punctuation">,</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;company1=%#v\n&quot;</span><span class="token punctuation">,</span> company1<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br></div></div><h2 id="_9-嵌套匿名结构体"><a href="#_9-嵌套匿名结构体" class="header-anchor">#</a> 9 嵌套匿名结构体</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>

<span class="token comment">//Address 地址结构体</span>
<span class="token keyword">type</span> Address <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Province <span class="token builtin">string</span>
	City     <span class="token builtin">string</span>
<span class="token punctuation">}</span>

<span class="token comment">//User 用户结构体</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>
	Gender  <span class="token builtin">string</span>
	 Address <span class="token comment">//匿名字段</span>
<span class="token punctuation">}</span>

<span class="token comment">//company 公司结构体</span>
<span class="token keyword">type</span> company <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name    <span class="token builtin">string</span>
	 Address
<span class="token punctuation">}</span>


<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	user1 <span class="token operator">:=</span> User<span class="token punctuation">{</span>
		Name<span class="token punctuation">:</span>   <span class="token string">&quot;刘强东&quot;</span><span class="token punctuation">,</span>
		Gender<span class="token punctuation">:</span> <span class="token string">&quot;男&quot;</span><span class="token punctuation">,</span>
		Address<span class="token punctuation">:</span> Address<span class="token punctuation">{</span>
			Province<span class="token punctuation">:</span> <span class="token string">&quot;江苏&quot;</span><span class="token punctuation">,</span>
			City<span class="token punctuation">:</span>     <span class="token string">&quot;宿迁&quot;</span><span class="token punctuation">,</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;user1=%#v\n&quot;</span><span class="token punctuation">,</span> user1<span class="token punctuation">)</span>
	<span class="token comment">//嵌套结构体访问属性</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>user1<span class="token punctuation">.</span>Address<span class="token punctuation">.</span>City<span class="token punctuation">)</span>
	<span class="token comment">//匿名嵌套结构体访问属性简写</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>user1<span class="token punctuation">.</span>City<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><p>当访问结构体成员时会先在结构体中查找该字段，找不到再去匿名结构体中查找。</p> <h2 id="_10-嵌套结构体的字段名冲突"><a href="#_10-嵌套结构体的字段名冲突" class="header-anchor">#</a> 10 嵌套结构体的字段名冲突</h2> <blockquote><p>嵌套结构体内部可能存在相同的字段名。这个时候为了避免歧义需要指定具体的内嵌结构体的字段。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//Address 地址结构体</span>
<span class="token keyword">type</span> Address <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Province   <span class="token builtin">string</span>
	City       <span class="token builtin">string</span>
	CreateTime <span class="token builtin">string</span>
<span class="token punctuation">}</span>

<span class="token comment">//Email 邮箱结构体</span>
<span class="token keyword">type</span> Email <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Account    <span class="token builtin">string</span>
	CreateTime <span class="token builtin">string</span>
<span class="token punctuation">}</span>

<span class="token comment">//User 用户结构体</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name   <span class="token builtin">string</span>
	Gender <span class="token builtin">string</span>
	Address
	Email
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> user3 User
	user3<span class="token punctuation">.</span>Name <span class="token operator">=</span> <span class="token string">&quot;沙河娜扎&quot;</span>
	user3<span class="token punctuation">.</span>Gender <span class="token operator">=</span> <span class="token string">&quot;男&quot;</span>
	<span class="token comment">// user3.CreateTime = &quot;2019&quot; //ambiguous selector user3.CreateTime</span>
	user3<span class="token punctuation">.</span>Address<span class="token punctuation">.</span>CreateTime <span class="token operator">=</span> <span class="token string">&quot;2000&quot;</span> <span class="token comment">//指定Address结构体中的CreateTime</span>
	user3<span class="token punctuation">.</span>Email<span class="token punctuation">.</span>CreateTime <span class="token operator">=</span> <span class="token string">&quot;2000&quot;</span>   <span class="token comment">//指定Email结构体中的CreateTime</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h2 id="_11-结构体与json序列化"><a href="#_11-结构体与json序列化" class="header-anchor">#</a> 11 结构体与JSON序列化</h2> <blockquote><p>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。JSON键值对是用来保存JS对象的一种方式，键/值对组合中的键名写在前面并用双引号&quot;&quot;包裹，使用冒号:分隔，然后紧接着值；多个键值之间使用英文,分隔。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
 
<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;encoding/json&quot;</span>
	<span class="token string">&quot;fmt&quot;</span>
<span class="token punctuation">)</span>
 
<span class="token comment">//属性必须大写，不然json解析不出来</span>
<span class="token keyword">type</span> Person <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Name <span class="token builtin">string</span>
	Age <span class="token builtin">int</span>
	Sex <span class="token builtin">string</span>
	Hobby <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>
<span class="token punctuation">}</span>
 
<span class="token keyword">var</span> wek<span class="token punctuation">,</span>alice Person
<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//初始化两个结构体</span>
	wek <span class="token operator">=</span> Person<span class="token punctuation">{</span><span class="token string">&quot;wek&quot;</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token string">&quot;男&quot;</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;alice&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;play&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
	alice <span class="token operator">=</span> Person<span class="token punctuation">{</span><span class="token string">&quot;alice&quot;</span><span class="token punctuation">,</span><span class="token number">17</span><span class="token punctuation">,</span><span class="token string">&quot;女&quot;</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;wek&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;study&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;teacher&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
 
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//序列化wek结构体</span>
	wekBytes<span class="token punctuation">,</span> e <span class="token operator">:=</span> json<span class="token punctuation">.</span><span class="token function">Marshal</span><span class="token punctuation">(</span>wek<span class="token punctuation">)</span>
	<span class="token keyword">if</span> e<span class="token operator">!=</span><span class="token boolean">nil</span><span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;结构体序列化失败！&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//打印wek序列化结构体</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;序列化结果为：&quot;</span><span class="token punctuation">,</span>wekBytes<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;序列化结果转化为string后为：&quot;</span><span class="token punctuation">,</span><span class="token function">string</span><span class="token punctuation">(</span>wekBytes<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">//序列化alice结构体</span>
	aliceBytes<span class="token punctuation">,</span> e <span class="token operator">:=</span> json<span class="token punctuation">.</span><span class="token function">Marshal</span><span class="token punctuation">(</span>alice<span class="token punctuation">)</span>
	<span class="token keyword">if</span> e<span class="token operator">!=</span><span class="token boolean">nil</span><span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;结构体序列化失败！&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//打印alice序列化结果</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;序列化结果为：&quot;</span><span class="token punctuation">,</span>aliceBytes<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;序列化结果转化为string后为：&quot;</span><span class="token punctuation">,</span><span class="token function">string</span><span class="token punctuation">(</span>aliceBytes<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><p>输出结果</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>序列化结果为： <span class="token punctuation">[</span><span class="token number">123</span> <span class="token number">34</span> <span class="token number">78</span> <span class="token number">97</span> <span class="token number">109</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">34</span> <span class="token number">119</span> <span class="token number">101</span> <span class="token number">107</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">65</span> <span class="token number">103</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">49</span> <span class="token number">56</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">83</span> <span class="token number">101</span> <span class="token number">120</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">34</span> <span class="token number">231</span> <span class="token number">148</span> <span class="token number">183</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">72</span> <span class="token number">111</span> <span class="token number">98</span> <span class="token number">98</span> <span class="token number">121</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">91</span> <span class="token number">34</span> <span class="token number">97</span> <span class="token number">108</span> <span class="token number">105</span> <span class="token number">99</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">112</span> <span class="token number">108</span> <span class="token number">97</span> <span class="token number">121</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">99</span> <span class="token number">111</span> <span class="token number">100</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">93</span> <span class="token number">125</span><span class="token punctuation">]</span>
序列化结果转化为<span class="token builtin">string</span>后为： <span class="token punctuation">{</span><span class="token string">&quot;Name&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;wek&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Age&quot;</span><span class="token punctuation">:</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token string">&quot;Sex&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;男&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Hobby&quot;</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">&quot;alice&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;play&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
序列化结果为： <span class="token punctuation">[</span><span class="token number">123</span> <span class="token number">34</span> <span class="token number">78</span> <span class="token number">97</span> <span class="token number">109</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">34</span> <span class="token number">97</span> <span class="token number">108</span> <span class="token number">105</span> <span class="token number">99</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">65</span> <span class="token number">103</span> <span class="token number">101</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">49</span> <span class="token number">55</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">83</span> <span class="token number">101</span> <span class="token number">120</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">34</span> <span class="token number">229</span> <span class="token number">165</span> <span class="token number">179</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">72</span> <span class="token number">111</span> <span class="token number">98</span> <span class="token number">98</span> <span class="token number">121</span> <span class="token number">34</span> <span class="token number">58</span> <span class="token number">91</span> <span class="token number">34</span> <span class="token number">119</span> <span class="token number">101</span> <span class="token number">107</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">115</span> <span class="token number">116</span> <span class="token number">117</span> <span class="token number">100</span> <span class="token number">121</span> <span class="token number">34</span> <span class="token number">44</span> <span class="token number">34</span> <span class="token number">116</span> <span class="token number">101</span> <span class="token number">97</span> <span class="token number">99</span> <span class="token number">104</span> <span class="token number">101</span> <span class="token number">114</span> <span class="token number">34</span> <span class="token number">93</span> <span class="token number">125</span><span class="token punctuation">]</span>
序列化结果转化为<span class="token builtin">string</span>后为： <span class="token punctuation">{</span><span class="token string">&quot;Name&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;alice&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Age&quot;</span><span class="token punctuation">:</span><span class="token number">17</span><span class="token punctuation">,</span><span class="token string">&quot;Sex&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;女&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Hobby&quot;</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">&quot;wek&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;study&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;teacher&quot;</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_12-结构体标签（tag）"><a href="#_12-结构体标签（tag）" class="header-anchor">#</a> 12 结构体标签（Tag）</h2> <blockquote><p>Tag是结构体的元信息，可以在运行的时候通过反射的机制读取出来。 Tag在结构体字段的后方定义，由一对反引号包裹起来，具体的格式如下</p></blockquote> <div class="custom-block danger"><p class="custom-block-title">总结</p> <ul><li>golang是非面向对象语言，也可以说go语言中的结构体类似java中的类，但是很明显缺少继承多态等等OO的特性</li> <li>指针变量通过.访问结构体成员，如果是C或者C++一定要通过*访问，这是Go对它的一个优化</li></ul></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/learn-go/24 Go语言常用方法总结.html" class="prev">
        Go常用方法总结
      </a></span> <span class="next"><a href="/go/learn-go/26 Go语言方法.html">
        Go语言方法
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/79.2c7d6b79.js" defer></script>
  </body>
</html>
